"""
Case Type   : 事件触发器
Case Name   : 【事件触发器】创建事件触发器，指定触发多命令支持事件触发器
Create At   :   2024/07/13
Owner       :   sungang14
Description :
    预置条件：
        1.创建事件触发器函数：
        CREATE OR REPLACE FUNCTION f_eventtrigger_0042()
        RETURNS event_trigger
        LANGUAGE plpgsql
        AS $$ BEGIN
        RAISE NOTICE 'test_event_trigger: % %', tg_event, tg_tag;
        END;
        $$;
    测试步骤：
        1.创建事件触发器
        CREATE EVENT TRIGGER eventtri_eventtrigger_0042 ON ddl_command_start WHEN TAG IN ('CREATE TABLE','ALTER TABLE','DROP TABLE') EXECUTE PROCEDURE f_eventtrigger_0042();
        2.创建表
        create table t_eventtrigger_0042(id int);
        3.修改表
        alter table t_eventtrigger_0042 rename to t_eventtrigger_0042_new;
        4.删除表
        drop table t_eventtrigger_0042_new;
        5、创建SEQUENCE
        create sequence s_eventtrigger_0042;
Expect      : 
        1.创建事件触发器成功
        2.创建表触发执行函数f_eventtrigger_0042()
        test_event_trigger: ddl_command_start,CREATE TABLE
        3.修改表触发执行函数f_eventtrigger_0042()
        test_event_trigger: ddl_command_start,ALTER TABLE
        4.删除表触发执行函数f_eventtrigger_0042()
        test_event_trigger: ddl_command_start,DROP TABLE
        5、不触发执行函数f_eventtrigger_0042
History     : 
"""

import sys
import os
import unittest
from yat.test import Node
from yat.test import macro

sys.path.append(sys.path[0] + "/../")
from testcase.utils.Logger import Logger
from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH


class IndexFileDamaged(unittest.TestCase):
    def setUp(self):
        self.logger = Logger()
        self.logger.info(f'-----{os.path.basename(__file__)} start-----')
        self.logger.info('----------------this is setup-----------------------')
        self.DB_ENV_PATH = macro.DB_ENV_PATH
        self.DB_INSTANCE_PATH = macro.DB_INSTANCE_PATH
        self.comsh = CommonSH('PrimaryDbBmode')
        self.comsh.execut_db_sql("drop EVENT TRIGGER if exists eventtri_eventtrigger_0042;")
        self.comsh.execut_db_sql("drop TABLE if exists t_eventtrigger_0042;")
        self.comsh.execut_db_sql("drop TABLE if exists t_eventtrigger_0042_new;")
        self.comsh.execut_db_sql("drop SEQUENCE if exists s_eventtrigger_0042;")
        self.comsh.execut_db_sql("drop function if exists f_eventtrigger_0042();")
        msg = self.comsh.execut_db_sql(
            f"CREATE OR REPLACE FUNCTION f_eventtrigger_0042() RETURNS event_trigger LANGUAGE plpgsql" \
            f" AS \$\$ BEGIN" \
            f" RAISE NOTICE 'test_event_trigger: % %', tg_event, tg_tag;" \
            f"END;" \
            f"\$\$;")
        self.logger.info(msg)
        self.assertIn("CREATE FUNCTION", msg)

    def test_Index_file_damaged(self):
        self.logger.info(
            "创建事件触发器:CREATE EVENT TRIGGER eventtri_eventtrigger_0042 ON ddl_command_start WHEN TAG IN ('CREATE TABLE','ALTER TABLE','DROP TABLE') EXECUTE PROCEDURE f_eventtrigger_0042();")
        msg = self.comsh.execut_db_sql(
            "CREATE EVENT TRIGGER eventtri_eventtrigger_0042 ON ddl_command_start WHEN TAG IN ('CREATE TABLE','ALTER TABLE','DROP TABLE') EXECUTE PROCEDURE f_eventtrigger_0042();")
        self.logger.info(msg)
        self.assertEqual(f'CREATE EVENT TRIGGER', msg)

        self.logger.info("创建表:create table t_eventtrigger_0042(id int);")
        msg = self.comsh.execut_db_sql("create table t_eventtrigger_0042(id int);")
        self.logger.info(msg)
        self.assertIn(f'test_event_trigger: ddl_command_start CREATE TABLE', msg)
        self.assertIn(f'CREATE TABLE', msg)

        self.logger.info("修改表:alter table t_eventtrigger_0042 rename to t_eventtrigger_0042_new;")
        msg = self.comsh.execut_db_sql("alter table t_eventtrigger_0042 rename to t_eventtrigger_0042_new;")
        self.logger.info(msg)
        self.assertIn(f'test_event_trigger: ddl_command_start ALTER TABLE', msg)
        self.assertIn(f'ALTER TABLE', msg)

        self.logger.info("删除表:drop table t_eventtrigger_0042_new;")
        msg = self.comsh.execut_db_sql("drop table t_eventtrigger_0042_new;")
        self.logger.info(msg)
        self.assertIn(f'test_event_trigger: ddl_command_start DROP TABLE', msg)
        self.assertIn(f'DROP TABLE', msg)

        self.logger.info("创建SEQUENCE:create sequence s_eventtrigger_0042;")
        msg = self.comsh.execut_db_sql("create sequence s_eventtrigger_0042;")
        self.logger.info(msg)
        self.assertIn(f'CREATE SEQUENCE', msg)

    def tearDown(self):
        self.logger.info('----------------this is tearDown-----------------------')
        # 删除事件触发器
        msg = self.comsh.execut_db_sql("drop EVENT TRIGGER if exists eventtri_eventtrigger_0042;")
        self.logger.info(msg)
        self.assertIn('DROP EVENT TRIGGER', msg)
        # 删除事件触发器函数
        msg = self.comsh.execut_db_sql("drop function if exists f_eventtrigger_0042();")
        self.logger.info(msg)
        self.assertIn('DROP FUNCTION', msg)
        # 删除SEQUENCE
        msg = self.comsh.execut_db_sql("drop SEQUENCE if exists s_eventtrigger_0042;")
        self.logger.info(msg)
        self.assertIn('DROP SEQUENCE', msg)
        self.logger.info(f'-----{os.path.basename(__file__)} end-----')
